using System.IO;
using System.Reflection;
using GrapeCity.Documents.Excel;
using System;

namespace GrapeCity.Documents.Excel.Examples.Templates.DataSource.JsonFile
{
    class Program
    {
        static void Main(string[] args)
        {
			//create a new workbook
			var workbook = new GrapeCity.Documents.Excel.Workbook();
			
			//Load template file Template_FamilyInfo.xlsx from resource
			var templateFile = GetResourceStream("xlsx\\Template_FamilyInfo.xlsx");
			workbook.Open(templateFile);
			
			//Get data from json file
			string jsonText = string.Empty;
			using (Stream stream = GetResourceStream("Template_FamilyInfo.json"))
			using (StreamReader reader = new StreamReader(stream))
			{
			    jsonText = reader.ReadToEnd();
			}
			// Create a JsonDataSource
			var datasource = new JsonDataSource(jsonText);
			
			//Add data source
			workbook.AddDataSource("ds", datasource);
			//Invoke to process the template
			workbook.ProcessTemplate();
			        
			// Save to an excel file
			workbook.Save("JsonFile.xlsx");

        }

		static Stream GetResourceStream(string resourcePath)
        {
            string resource = "JsonFile.Resource." + resourcePath.Replace("\\", ".");
            var assembly = typeof(Program).GetTypeInfo().Assembly;
            return assembly.GetManifestResourceStream(resource);
        }

    }
}